package org.urchr.beorganized.api.base;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet implements Filter {
       
	private ServletContext servletContext = null;
	
	private static final long serialVersionUID = -2822332334063729388L;

	private void doProcessRequestInternal(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//		System.out.println("["+new Date()+"]class = "+request.getServletPath());
		try {
			request.setCharacterEncoding("utf-8");
			BaseAPIServlet instance = 
				(BaseAPIServlet) Class.forName("org.urchr.beorganized.api.impl."+request.getServletPath().substring(5)).newInstance();
			instance.doProcessRequestInternal(request, response, servletContext);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendError(500);
		}
	}

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		doProcessRequestInternal((HttpServletRequest)request, (HttpServletResponse)response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		servletContext = arg0.getServletContext();
	}
	
}
